import {Type} from '@angular/core';
import {BladeContainerInterface} from './blade-container.interface';
import {BladeData} from './blade-data';
import {ToolbarService} from '../toolbar.service';
import {BladeService} from '../blade.service';
import {DialogService} from '../dialog.service';

/**
 * 弹出层实体对象
 *
 * @export
 * @class Blade
 */
export class Blade {
  /**
   * 当前对象实体
   *
   * @type {*}
   * @memberof Blade
   */
  currentEntity: any;
  /**
   * 当前父对象实体
   *
   * @type {*}
   * @memberof Blade
   */
  parentEntity: any;
  /**
   * 弹出层数据
   *
   * @type {BladeData}
   * @memberof Blade
   */
  bladeData: BladeData;
  /**
   * 弹出层父对象
   *
   * @type {Blade}
   * @memberof Blade
   */
  parentBlade: Blade;
  /**
   * 子弹出层数组
   *
   * @type {Blade[]}
   * @memberof Blade
   */
  childrenBlades: Blade[] = [];
  /**
   * 对话框服务对象
   *
   * @type {DialogService}
   * @memberof Blade
   */
  dialogService: DialogService;
  /**
   * 工具条服务对象
   *
   * @type {ToolbarService}
   * @memberof Blade
   */
  toolbarService: ToolbarService;
  /**
   * 弹出层服务对象
   *
   * @type {BladeService}
   * @memberof Blade
   */
  bladeService: BladeService;
  /**
   * 弹出层内容对象实例
   *
   * @type {*}
   * @memberof Blade
   */
  bladeContainerInstance: BladeContainerInterface;
  /**
   * 标示创建或者编辑对象
   *
   * @type {boolean}
   * @memberof Blade
   */
  isNew: boolean;

  /**
   * 创建一个弹出层对象.
   * @param {Type<any>} bladeContainerType 弹出层内容对象类型
   *
   * @memberof Blade
   */
  constructor(
    public bladeContainerType: Type<any>) {
  }

  childrenSaveChange() {
    if (this.childrenBlades.length > 0) {
      for (let i = 0; i < this.childrenBlades.length; i++) {
        if (!this.childrenBlades[i].bladeContainerInstance.saveChange()) {
          return false;
        }
      }
    }
  }

  /**
   * 子弹出层是否存在未提交的变更
   *
   * @returns {boolean}
   *
   * @memberof Blade
   */
  childrenHasChange(): boolean {
    if (this.childrenBlades.length > 0) {
      for (let i = 0; i < this.childrenBlades.length; i++) {
        if (!this.childrenBlades[i].bladeContainerInstance.hasChange()) {
          return false;
        }
      }
    }
    return true;
  }

  /**
   * 子弹出框是否存在验证错误
   *
   * @returns {boolean}
   *
   * @memberof Blade
   */
  childrenHasError(): boolean {
    if (this.childrenBlades.length > 0) {
      for (let i = 0; i < this.childrenBlades.length; i++) {
        if (!this.childrenBlades[i].bladeContainerInstance.hasError()) {
          return false;
        }
      }
    }
    return true;
  }

  /**
   * 子弹出框是否可以离开
   *
   * @returns {boolean}
   *
   * @memberof Blade
   */
  childrenCanDeactivate(): boolean {
    if (this.childrenBlades.length > 0) {
      for (let i = 0; i < this.childrenBlades.length; i++) {
        if (!this.childrenBlades[i].bladeContainerInstance.canDeactivate()) {
          return false;
        }
      }
    }
    return true;
  }
}


// WEBPACK FOOTER //
// ./src/app/shared/blade/blade.model.ts
